package com.bdyreptile.bdyreptile;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * html处理
 */
@RestController
@RequestMapping("html")
class HtmlController {

    @RequestMapping("text")
    public String toText(String text){
        Document parse = Jsoup.parse(text);
        Elements select = parse.select(".filename");
        //获取字符串集合
        ArrayList<String> list = toStrList(select);
        //排序
        toSort(list);
        //list转字符串
        return toText(list);
    }


    //1.1element转字符串list
    private ArrayList<String> toStrList(Elements select) {
        ArrayList<String> list = new ArrayList<String>();
        for (Element element : select) {
            list.add(element.attr("title").replaceAll(".mp4","")+"\r\n");
        }
        return list;
    }
    //1.2排序
    private void toSort(ArrayList<String> list) {
        try {
            Collections.sort(list,new StringComparatorUtils());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //1.2.1获取字符串中的数字
    public Integer getNumber(String content){
        //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
        String regEx="[^0-9]+";
        Pattern pattern = Pattern.compile(regEx);
        //用定义好的正则表达式拆分字符串，，把字符串中的数字留出来
        String[] arr = pattern.split(content);
        if(arr.length==0){return -1;}
        return Integer.parseInt(arr[1]);
    }
    //1.3字符串集合转string
    private String toText(ArrayList<String> list) {
        String result = "";
        for (String string : list) {
            result+=string;
        }
        System.out.println(result);
        return result;
    }



}
